/*
 * gpio.c
 * Project 03
 * Nicole Ang (nang1@asu.edu)
 * Bijan Fakhri (bfakhri@asu.edu)
 * CSE325 Embedded Microprocessor Systems
 * Spring 2013
 */

#include "gpio.h"

/* Accesses the SETDD register and returns the state of pin p_pin (0 or 1). */
//Getting state of a DIP switch
int gpio_port_dd_get_pin_state(int p_pin){
	int shift = 3 + p_pin;
	int bit = 1 << shift;
	return ((MCF_GPIO_SETDD & bit) >> shift) & 0x01;
}

/* Configures PDDPAR and DDRDD so pin p_pin of port DD is in GPIO
 * function and the pin data direction is input. */
//Initializing DIP switches
void gpio_port_dd_init(){
	//Set PDDPAR pins to 0 for GPIO functions
	MCF_GPIO_PDDPAR &= ~(MCF_GPIO_PDDPAR_DDATA0_DDATA0 | MCF_GPIO_PDDPAR_DDATA1_DDATA1 
			| MCF_GPIO_PDDPAR_DDATA2_DDATA2 | MCF_GPIO_PDDPAR_DDATA3_DDATA3);
	
	//set DDRDD pins to 0 to configure as input
	MCF_GPIO_DDRDD &= ~(MCF_GPIO_DDRDD_DDRDD4 | MCF_GPIO_DDRDD_DDRDD5 |
			MCF_GPIO_DDRDD_DDRDD6 | MCF_GPIO_DDRDD_DDRDD7);
}

/* Accesses the SETTC register and returns the state of pin p_pin (0 or 1). */
//Getting the state of an LED
int gpio_port_tc_get_pin_state(int p_pin){
	int shift = p_pin - 1;
	int bit = 1 << shift;
	return ((MCF_GPIO_SETTC & bit) >> shift) & 0x1;
}

/* Configures PTCPAR and DDRTC so port TC is in GPIO function
 * and the pin data direction is output. */
//Initializing LEDs
void gpio_port_tc_init(){
	//Set PTCPAR pins to 00 for GPIO function
	MCF_GPIO_PTCPAR &= MCF_GPIO_PTCPAR_PTCPAR0(MCF_GPIO_PTCPAR_DTIN0_GPIO)|
			MCF_GPIO_PTCPAR_PTCPAR1(MCF_GPIO_PTCPAR_DTIN1_GPIO) |
			MCF_GPIO_PTCPAR_PTCPAR2(MCF_GPIO_PTCPAR_DTIN2_GPIO) |
			MCF_GPIO_PTCPAR_PTCPAR3(MCF_GPIO_PTCPAR_DTIN3_GPIO);
	
	//Set DDTRC pins to 1 to configure as output
	MCF_GPIO_DDRTC |= MCF_GPIO_DDRTC_DDRTC0 | MCF_GPIO_DDRTC_DDRTC1 |
			MCF_GPIO_DDRTC_DDRTC2 | MCF_GPIO_DDRTC_DDRTC3;
}

/* Accesses the SETTC register to set the state of pin p_pin to p_state. */
//Setting an LED
void gpio_port_tc_set_pin_state(int p_pin, int p_state){
	if(p_state) 
		uc_led_on(p_pin); //set pin to 1
	else
		uc_led_off(p_pin); //set pin to 0
}